﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Standard : System.Web.UI.Page
{
    ILE_Global ileGlobal = new ILE_Global();
    ILE_Config ileConfig = new ILE_Config();

    protected int CourseID
    {
        get
        {
            return (int)ViewState["CourseID"];
        }
        set
        {
            ViewState["CourseID"] = value;
        }
    }

    protected int SectionNumber
    {
        get
        {
            return (int)ViewState["SectionNumber"];
        }
        set
        {
            ViewState["SectionNumber"] = value;
        }
    }

    protected int StandardID
    {
        get
        {
            return (int)ViewState["StandardID"];
        }
        set
        {
            ViewState["StandardID"] = value;
        }
    }

    ClassBLL classLogic = new ClassBLL();
    ClassStandardsBLL classStandardsLogic = new ClassStandardsBLL();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (Page.IsPostBack == false)
        {
            try
            {
                CourseID = Convert.ToInt32(Request.QueryString.Get("CourseID"));
                SectionNumber = Convert.ToInt32(Request.QueryString.Get("SectionNumber"));
                StandardID = Convert.ToInt32(Request.QueryString.Get("StandardID"));
            }
            catch
            {
                // *SteveMsg
                string modalPopupMsg = "Invalid CourseID, SectionNumber, or StandardID<br />You will be redirected to your My Classes page.";
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", modalPopupMsg, 2, "MyClasses.aspx");
            }

            if (CourseID == 0 || SectionNumber == 0 || StandardID == 0)
            {
                // *SteveMsg
                string modalPopupMsg = "Invalid Access. This Course or Standard has been disabled.";
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", modalPopupMsg, 2, "MyClasses.aspx");
                return;
            }

            Classes.CoursesRow course = classLogic.GetCourse(CourseID);
            Classes.Courses_SectionRow section = classLogic.GetSection(CourseID, SectionNumber);

            if (course == null || section == null)
            {
                // *SteveMsg
                string modalPopupMsg = "Invalid CourseID or SectionNumber.<br />You will be redirected to your My Classes page.";
                ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", modalPopupMsg, 2, "MyClasses.aspx");
                return;
            }
            else
            {
                if (!IsClassEnabled() || !IsStandardEnabled())
                {
                    // *SteveMsg
                    string modalPopupMsg = "Invalid Access. This Course or Standard has been disabled.";
                    ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", modalPopupMsg, 2, "MyClasses.aspx");
                    return;
                }
            }

            LoadCourseInfo();
            lblStandard.Text = ileGlobal.GetStandardNameByStandardID(StandardID);
            Page.Title = ileGlobal.GetStandardNameByStandardID(StandardID) + " for " + course.Title.ToString() + " [Section " + SectionNumber.ToString() + "]";
            lnkGoBackToClassPage.NavigateUrl = "Class.aspx?CourseID=" + CourseID.ToString() + "&SectionNumber=" + SectionNumber.ToString();

            LoadIntroduction();
            LoadLearningObjects();
            LoadAssignments();
            LoadPastAssignments();
        }
    }

    private bool IsClassEnabled()
    {
        Classes.CoursesRow course = classLogic.GetCourse(CourseID);
        Classes.Courses_SectionRow section = classLogic.GetSection(CourseID, SectionNumber);

        if (course.IsEnabled == true)
        {
            if (section.UseSectionInfo)
            {
                if (section.IsEnabled)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        else
        {
            return false;
        }
    }

    private bool IsStandardEnabled()
    {
        ClassStandards.Courses_StandardRow classStandard = classStandardsLogic.GetClassStandard(CourseID, StandardID);
        ClassStandards.Courses_Standard_SectionRow sectionStandard = classStandardsLogic.GetClassSectionStandard(CourseID, SectionNumber, StandardID);

        if (classStandard.IsStandardEnabled)
        {
            if (sectionStandard == null)
            {
                return true;
            }

            if (sectionStandard.UseSectionInfo)
            {
                if (sectionStandard.IsStandardEnabled)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            return true;
        }
        else
        {
            return false;
        }

    }

    private void LoadCourseInfo()
    {
        Classes.CoursesRow course = classLogic.GetCourse(CourseID);
        Classes.Courses_SectionRow section = classLogic.GetSection(CourseID, SectionNumber);

        if (section.UseSectionInfo != true)
        {
            // USE CLASS DEFAULT INFORMATION
            lblCourseTitle.Text = course.Title.ToString() + " - " + course.Subtitle.ToString();
            lblCourseSection.Text = "Section " + SectionNumber.ToString();
        }
        else
        {
            // USE SECTION INFORMATION
            lblCourseTitle.Text = course.Title.ToString() + " - " + section.Subtitle.ToString();
            lblCourseSection.Text = "Section " + SectionNumber.ToString();
        }
    }


    private void LoadIntroduction()
    {
        ClassStandards.Courses_StandardRow classStandard = classStandardsLogic.GetClassStandard(CourseID, StandardID);
        ClassStandards.Courses_Standard_SectionRow sectionStandard = classStandardsLogic.GetClassSectionStandard(CourseID, SectionNumber, StandardID);

        string IntroductionTitle = String.Empty;
        string Introduction = String.Empty;

        if (sectionStandard == null)
        {
            if (classStandard.IsIntroEnabled)
            {
                lblIntroductionTitle.Text = classStandard.IntroTitle;
                lblIntroduction.Text = classStandard.IntroContent;
                PanelIntroduction.Visible = true;
            }
            else
            {
                PanelIntroduction.Visible = false;
            }
        }
        else
        {
            if (sectionStandard.UseSectionInfo)
            {
                if (sectionStandard.IsIntroEnabled)
                {
                    lblIntroductionTitle.Text = sectionStandard.IntroTitle;
                    lblIntroduction.Text = sectionStandard.IntroContent;
                    PanelIntroduction.Visible = true;
                }
                else
                {
                    PanelIntroduction.Visible = false;
                }
            }
            else
            {
                if (classStandard.IsIntroEnabled)
                {
                    lblIntroductionTitle.Text = classStandard.IntroTitle;
                    lblIntroduction.Text = classStandard.IntroContent;
                    PanelIntroduction.Visible = true;
                }
                else
                {
                    PanelIntroduction.Visible = false;
                }
            }
        }
    }

    private void LoadLearningObjects()
    {
        ClassStandards.Courses_StandardRow classStandard = classStandardsLogic.GetClassStandard(CourseID, StandardID);
        ClassStandards.Courses_Standard_SectionRow sectionStandard = classStandardsLogic.GetClassSectionStandard(CourseID, SectionNumber, StandardID);

        if (sectionStandard == null)
        {
            ClassStandards.Courses_LearnObjDataTable coursesLearningObjs = classStandardsLogic.GetClassLearnObjs(CourseID, StandardID);
            RepeaterLearningObjects.DataSource = coursesLearningObjs.Rows;

            if (coursesLearningObjs.Count == 0)
            {
                // *SteveMsg
                lblNoLearnObjs.Text = "<h5>No Learning Object</h5>";
                lblNoLearnObjs.Visible = true;
            }
            else
            {
                lblNoLearnObjs.Visible = false;
            }
        }
        else
        {
            if (!sectionStandard.UseSectionInfo)
            {
                ClassStandards.Courses_LearnObjDataTable coursesLearningObjs = classStandardsLogic.GetClassLearnObjs(CourseID, StandardID);
                RepeaterLearningObjects.DataSource = coursesLearningObjs.Rows;

                if (coursesLearningObjs.Count == 0)
                {
                    // *SteveMsg
                    lblNoLearnObjs.Text = "<h5>No Learning Object</h5>";
                    lblNoLearnObjs.Visible = true;
                }
                else
                {
                    lblNoLearnObjs.Visible = false;
                }
            }
            else
            {
                ClassStandards.Courses_LearnObj_SectionDataTable sectionsLearningObjs = classStandardsLogic.GetClassSectionLearnObjs(CourseID, SectionNumber, StandardID);
                RepeaterLearningObjects.DataSource = sectionsLearningObjs.Rows;

                if (sectionsLearningObjs.Count == 0)
                {
                    // *SteveMsg
                    lblNoLearnObjs.Text = "<h5>No Learning Object</h5>";
                    lblNoLearnObjs.Visible = true;
                }
                else
                {
                    lblNoLearnObjs.Visible = false;
                }
            }
        }

        RepeaterLearningObjects.DataBind();
    }

    protected void RepeaterLearningObjects_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            LearnObjsBLL learnobjLogic = new LearnObjsBLL();

            ClassStandards.Courses_StandardRow classStandard = classStandardsLogic.GetClassStandard(CourseID, StandardID);
            ClassStandards.Courses_Standard_SectionRow sectionStandard = classStandardsLogic.GetClassSectionStandard(CourseID, SectionNumber, StandardID);

            HyperLink lnkLearningObject = e.Item.FindControl("lnkLearningObject") as HyperLink;
            Label lblLearningObjectEstimatedTime = e.Item.FindControl("lblLearningObjectEstimatedTime") as Label;

            if (sectionStandard == null)
            {
                ClassStandards.Courses_LearnObjRow row = e.Item.DataItem as ClassStandards.Courses_LearnObjRow;

                if (!row.UseAlterInfo)
                {
                    if (row.IsEnabled)
                    {
                        LearnObjs.LearnObjsRow learnobj = learnobjLogic.GetLearnObj(row.LearnObjID);
                        lnkLearningObject.Text = learnobj.Title;
                    }
                }
                else
                {
                    if (row.IsEnabled)
                    {
                        lnkLearningObject.Text = row.AlterTitle;
                    }
                }

                lnkLearningObject.NavigateUrl = "LearningObject.aspx?CourseID=" + CourseID.ToString() +
                                                                    "&SectionNumber=" + SectionNumber.ToString() +
                                                                    "&StandardID=" + StandardID.ToString() +
                                                                    "&LearnObjID=" + row.LearnObjID.ToString();

                LearnObjs.LearnObjsRow learnobj3 = learnobjLogic.GetLearnObj(row.LearnObjID);
                lblLearningObjectEstimatedTime.Text = "(" + learnobj3.EstimatedTime + ")";
            }
            else if (!sectionStandard.UseSectionInfo)
            {
                ClassStandards.Courses_LearnObjRow row = e.Item.DataItem as ClassStandards.Courses_LearnObjRow;

                if (!row.UseAlterInfo)
                {
                    if (row.IsEnabled)
                    {
                        LearnObjs.LearnObjsRow learnobj = learnobjLogic.GetLearnObj(row.LearnObjID);
                        lnkLearningObject.Text = learnobj.Title;
                    }
                }
                else
                {
                    if (row.IsEnabled)
                    {
                        lnkLearningObject.Text = row.AlterTitle;
                    }
                }

                lnkLearningObject.NavigateUrl = "LearningObject.aspx?CourseID=" + CourseID.ToString() +
                                                                    "&SectionNumber=" + SectionNumber.ToString() +
                                                                    "&StandardID=" + StandardID.ToString() +
                                                                    "&LearnObjID=" + row.LearnObjID.ToString();

                LearnObjs.LearnObjsRow learnobj3 = learnobjLogic.GetLearnObj(row.LearnObjID);
                lblLearningObjectEstimatedTime.Text = "(" + learnobj3.EstimatedTime + ")";
            }
            else
            {
                ClassStandards.Courses_LearnObj_SectionRow row = e.Item.DataItem as ClassStandards.Courses_LearnObj_SectionRow;

                if (!row.UseAlterInfo)
                {
                    if (row.IsEnabled)
                    {
                        LearnObjs.LearnObjsRow learnobj = learnobjLogic.GetLearnObj(row.LearnObjID);
                        lnkLearningObject.Text = learnobj.Title;
                    }
                }
                else
                {
                    if (row.IsEnabled)
                    {
                        lnkLearningObject.Text = row.AlterTitle;
                    }
                }

                lnkLearningObject.NavigateUrl = "LearningObject.aspx?CourseID=" + CourseID.ToString() +
                                                                    "&SectionNumber=" + SectionNumber.ToString() +
                                                                    "&StandardID=" + StandardID.ToString() +
                                                                    "&LearnObjID=" + row.LearnObjID.ToString();

                LearnObjs.LearnObjsRow learnobj2 = learnobjLogic.GetLearnObj(row.LearnObjID);
                lblLearningObjectEstimatedTime.Text = "(" + learnobj2.EstimatedTime + ")";
            }
        }
    }

    private void LoadAssignments()
    {
        AssignmentsBLL assignmentsLogic = new AssignmentsBLL();

        Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByCourseIdAndSectionNumberAndStandardId(CourseID, SectionNumber, StandardID);
        Assignments.AssignmentsDataTable assignments = new Assignments.AssignmentsDataTable();

        foreach (Assignments.RelationsRow relation in relations)
        {
            Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(relation.AssignmentID);
            if (assignment.StartDateTime <= DateTime.Now && assignment.EndDateTime > DateTime.Now)
            {
                assignments.ImportRow(assignment);
            }
        }

        var sortedAssignment = assignments.OrderBy(p => (DateTime)p.EndDateTime).OrderByDescending(p => (string)p.AssignmentType);

        if (assignments.Count >= 1)
        {
            RepeaterAssignments.DataSource = sortedAssignment.ToList();
            RepeaterAssignments.DataBind();
            PanelAssignments.Visible = true;
        }
        else
        {
            PanelAssignments.Visible = false;
        }
    }

    protected void RepeaterAssignments_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();

            Assignments.AssignmentsRow row = e.Item.DataItem as Assignments.AssignmentsRow;

            HyperLink lnkAssignment = e.Item.FindControl("lnkAssignment") as HyperLink;
            Label lblDueDate = e.Item.FindControl("lblDueDate") as Label;

            lnkAssignment.Text = row.AssignmentTitle;
            lnkAssignment.NavigateUrl = "Assignment.aspx?CourseID=" + CourseID.ToString() +
                "&SectionNumber=" + SectionNumber.ToString() +
                "&StandardID=" + StandardID.ToString() +
                "&AssignmentID=" + row.AssignmentID.ToString();

            lblDueDate.Text = "Due on ";
            lblDueDate.Text += row.EndDateTime.ToString("f");
        }
    }

    private void LoadPastAssignments()
    {
        AssignmentsBLL assignmentsLogic = new AssignmentsBLL();

        Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByCourseIdAndSectionNumberAndStandardId(CourseID, SectionNumber, StandardID);
        Assignments.AssignmentsDataTable assignments = new Assignments.AssignmentsDataTable();

        foreach (Assignments.RelationsRow relation in relations)
        {
            Assignments.AssignmentsRow assignment = assignmentsLogic.GetAssignmentByAssignmentID(relation.AssignmentID);
            if (assignment.EndDateTime <= DateTime.Now)
            {
                assignments.ImportRow(assignment);
            }
        }

        var sortedPastAssignment = assignments.OrderBy(p => (DateTime)p.EndDateTime).OrderByDescending(p => (string)p.AssignmentType);

        if (assignments.Count >= 1)
        {
            RepeaterPastAssignments.DataSource = sortedPastAssignment.ToList();
            RepeaterPastAssignments.DataBind();
            PanelPastAssignments.Visible = true;
        }
        else
        {
            PanelPastAssignments.Visible = false;
        }
    }

    protected void RepeaterPastAssignments_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();

            Assignments.AssignmentsRow row = e.Item.DataItem as Assignments.AssignmentsRow;

            HyperLink lnkAssignment = e.Item.FindControl("lnkAssignment") as HyperLink;
            Label lblDueDate = e.Item.FindControl("lblDueDate") as Label;

            lnkAssignment.Text = row.AssignmentTitle;
            lnkAssignment.NavigateUrl = "Assignment.aspx?CourseID=" + CourseID.ToString() +
                "&SectionNumber=" + SectionNumber.ToString() +
                "&StandardID=" + StandardID.ToString() +
                "&AssignmentID=" + row.AssignmentID.ToString();

            lblDueDate.Text = "Due on ";
            lblDueDate.Text += row.EndDateTime.ToString("f");
        }
    }
}
